package com.xiaoyu.lanling.feature.chat.viewholder;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import androidx.cardview.widget.CardView;

import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.xiaoyu.base.app.AppContext;
import com.xiaoyu.im.datamodel.MessageDisplayStatus;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSendVideoItem;
import com.xiaoyu.lanling.feature.chat.viewholder.messageclick.ChatMessageVideoItemClickHandler;
import com.xiaoyu.lanling.widget.player.MessageVideoPlayer;

import org.jetbrains.annotations.NotNull;

public class ChatMessageSendVideoViewHolder extends ChatMessageBaseReceiveSendViewHolder<ChatMessageSendVideoItem> {

    private static final String TAG = ChatMessageSendVideoViewHolder.class.getSimpleName();
    private GSYVideoOptionBuilder mVideoOptionBuilder;

    private View mView;
    private ImageButton mFail;
    private QMUILoadingView mLoading;
    private CardView mVideoCardLayout;
    private MessageVideoPlayer mVideoPlayer;

    @Override
    protected View doCreateChildrenView(LayoutInflater layoutInflater, ViewGroup parent) {
        this.mView = layoutInflater.inflate(R.layout.item_chat_send_video, parent, false);
        this.mFail = mView.findViewById(R.id.chat_send_fail);
        this.mLoading = mView.findViewById(R.id.chat_send_loading);
        this.mVideoCardLayout = mView.findViewById(R.id.chat_message_video_card_layout);
        this.mVideoPlayer = mView.findViewById(R.id.chat_message_video_player);
        this.mVideoOptionBuilder = new GSYVideoOptionBuilder();
        return mView;
    }

    @Override
    public void showData(int position, @NotNull ChatMessageSendVideoItem itemData) {
        super.showData(position, itemData);
        final ChatMessageSendVideoItem chatMessageSendVideoItem = (ChatMessageSendVideoItem) itemData;
        if (chatMessageSendVideoItem.getMessageDisplayStatus() == MessageDisplayStatus.SENDING) {
            mLoading.setVisibility(View.VISIBLE);
            mFail.setVisibility(View.GONE);
        } else if (chatMessageSendVideoItem.getMessageDisplayStatus() == MessageDisplayStatus.NORMAL_FAIL) {
            mFail.setImageResource(R.drawable.icon_chat_send_fail);
            mFail.setVisibility(View.VISIBLE);
            mLoading.setVisibility(View.GONE);
        } else if (chatMessageSendVideoItem.getMessageDisplayStatus() == MessageDisplayStatus.BLACKED_FAIL) {
            mFail.setImageResource(R.drawable.icon_chat_check_fail);
            mFail.setVisibility(View.VISIBLE);
            mLoading.setVisibility(View.GONE);
        } else {
            mFail.setVisibility(View.GONE);
            mLoading.setVisibility(View.GONE);
        }
        mVideoCardLayout.getLayoutParams().width = chatMessageSendVideoItem.getWidth();
        mVideoCardLayout.getLayoutParams().height = chatMessageSendVideoItem.getHeight();
        mVideoCardLayout.requestLayout();

        ChatMessageVideoItemClickHandler.bindSendVideoItemLongClickForViewAndData(mVideoPlayer, chatMessageSendVideoItem);
        initVideoPlayer(chatMessageSendVideoItem);
    }

    private void initVideoPlayer(ChatMessageSendVideoItem item) {
        String url = item.getVideoUrl();
        if (url.startsWith("https")) {
            url = url.replace("https", "http");
        }
        mVideoOptionBuilder
                .setIsTouchWiget(false)
                .setUrl(url)
                .setVideoTitle("")
                .setIsTouchWigetFull(false)
                .setCacheWithPlay(true)
                .setRotateViewAuto(false)
                .setLockLand(false)
                .setPlayTag(TAG)
                .setLooping(false)
                .setShowFullAnimation(true)
                .setNeedLockFull(false)
                .setPlayPosition(mPosition)
                .setNeedShowWifiTip(false)
                .setThumbPlay(true)
                .setBottomProgressBarDrawable(AppContext.getDrawable(R.drawable.layer_video_progress))
                .setBottomShowProgressBarDrawable(AppContext.getDrawable(R.drawable.layer_video_progress), AppContext.getDrawable(R.drawable.selector_video_thump))
                .setVideoAllCallBack(new GSYSampleCallBack() {
                    @Override
                    public void onClickStartIcon(String url, Object... objects) {
                    }

                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                        if (!mVideoPlayer.isIfCurrentIsFullscreen()) {
                            GSYVideoManager.instance().setNeedMute(true);
                        }
                    }

                    @Override
                    public void onQuitFullscreen(String url, Object... objects) {
                        super.onQuitFullscreen(url, objects);
                        GSYVideoManager.instance().setNeedMute(true);
                    }

                    @Override
                    public void onEnterFullscreen(String url, Object... objects) {
                        super.onEnterFullscreen(url, objects);
                        GSYVideoManager.instance().setNeedMute(false);
                    }
                }).build(mVideoPlayer);

        mVideoPlayer.bindSendVideoItemViewAndDataForLongClick(item);
        mVideoPlayer.setDuration(item.getDuration());
        mVideoPlayer.loadCoverImage(item.getFirstPictureUrl(), item.getWidth(), item.getHeight());
        mVideoPlayer.getTitleTextView().setVisibility(View.GONE);
        mVideoPlayer.getBackButton().setVisibility(View.GONE);
    }

    @Override
    public void onViewDetachedFromWindow() {
        super.onViewDetachedFromWindow();
        if (mVideoPlayer.isIfCurrentIsFullscreen()) {
            GSYVideoManager.backFromWindowFull(mVideoPlayer.getContext());
        }
    }
}
